#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
//    ,_listen(nullptr)
//    ,_communtion(nullptr)
{
    ui->setupUi(this);
//    _listen=new QTcpServer(this);               //服务器监听套接字
//    _listen->listen(QHostAddress::Any,10001);   //监听
//    connect(_listen,SIGNAL(newConnection()),this,SLOT(newClientConnect()));
    mysql_init();
    connect(&admin1,SIGNAL(mySignal()),this,SLOT(ad_return()));
    connect(&ch,SIGNAL(mySignal()),this,SLOT(ch_return()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

//void MainWindow::newClientConnect()
//{
//    _communtion =_listen->nextPendingConnection();
//    QString ip=_communtion->peerAddress().toString();
//    qint16 port=_communtion->peerPort();
//    QString temp=QString("[%1,%2]已连接").arg(ip).arg(port);
//    qDebug()<<temp<<"客户端已经连接";
//    //ui->textEdit->setText(temp);
//    // mysql_init();
//}

void MainWindow::ad_return()
{
    admin1.hide();
    show();
}

void MainWindow::ch_return()
{
    ch.hide();
    show();
}

void MainWindow::mysql_init()        //数据库初始化
{
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("127.0.0.1");
    db.setUserName("root"); //数据库账号
    db.setPassword("123456");//数据库密码
    //db.setPort(3306);
    db.setDatabaseName("restaurtal"); //你的数据库名
    if(!db.open())
    {
        qDebug()<<"连接数据库失败";
    }else
    {
        qDebug()<<"连接数据库成功";
    }
}

void MainWindow::on_pushButton_clicked()
{
    if(ui->radioButton->isChecked())
    {
        //qDebug()<<"1";
        QString a=ui->lineEdit->text();
        QString b=ui->lineEdit_2->text();
        QSqlQuery query;
        QString sql=QString("select *from info where id=%1 and password='%2' and permit=1;").arg(a).arg(b);
        if(query.exec(sql))            //执行成功返回1
        {
            statusBar()->showMessage("success"+sql);
        }
        else
        {
            statusBar()->showMessage(query.lastError().text());     //错误返回错误信息
        }
        if(query.size()==0)
        {
            QMessageBox::information(this,"登录失败","pass或id错误");
        }
        else
        {
            qDebug()<<"管理员登录成功";
            hide();
            admin1.show();
        }
    }
    else if(ui->radioButton_2->isChecked())
    {
        //qDebug()<<"2";
        QString a=ui->lineEdit->text();
        QString b=ui->lineEdit_2->text();
        QSqlQuery query;
        QString sql=QString("select *from info where id=%1 and password='%2' and permit=0;").arg(a).arg(b);
        if(query.exec(sql))            //执行成功返回1
        {
            statusBar()->showMessage("success"+sql);
        }
        else
        {
            statusBar()->showMessage(query.lastError().text());     //错误返回错误信息
        }
        if(query.size()==0)
        {
            QMessageBox::information(this,"登录失败","pass或id错误");
        }
        else
        {
            qDebug()<<"服务员登录成功";
            hide();
            ch.show();
        }
    }

}
